package com.arkisv.common.interceptor;

import com.arkisv.common.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 分页拦截器
 * @author Feng King
 * @create 2017/1/16 14:47
 */
public class PageInterceptor extends HandlerInterceptorAdapter {
	/**
	 * 日志对象
	 */
	private static final Logger log = LoggerFactory.getLogger(PageInterceptor.class);
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav)
			throws Exception {
	}

	/**
	 * 分页拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 页码
		String pageNum = request.getParameter("pageNum");
		// 每页显示条数
		String pageSize = request.getParameter("pageSize");
		// 排序
		String orderBy = request.getParameter("orderBy");
		
		if(StringUtil.isNotBlank(pageNum)) {
			int ps = 10;
			if(StringUtil.isNotBlank(pageSize)) {
				try{
					ps = Integer.valueOf(pageSize);
				} catch (Exception e) {
				}
			}
			PageHelper.startPage(Integer.valueOf(pageNum), ps);
		}
		if(StringUtil.isNotBlank(orderBy)) {
			PageHelper.orderBy(orderBy);
		}
		
		return true;
	}

	
}
